package com.itheima.interceptor;

import com.itheima.controller.result.Result;
import com.itheima.system.exception.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Component
//@ControllerAdvice的意思是声明这个是一个controller的通知类,这样声明过后才会被加载为异常处理器
@ControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result doBusinessException(BusinessException ex){
        Result result=new Result();
        result.setCode(ex.getCode());
        String message = ex.getMessage();
        result.setData(message);
        return result;

    }
}
